﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;

namespace ActivityTracker.Library
{
    public class StringEnum
    {
        #region Instance implementation

        private Type _enumType;

        public StringEnum(Type enumType)
        {
            if (!enumType.IsEnum)
                throw new ArgumentException(String.Format("Supplied type must be an Enum.  Type was {0}", enumType.ToString()));

            _enumType = enumType;
        }

        public string GetStringValue(string valueName)
        {
            Enum enumType;
            string stringValue = null;
            try
            {
                enumType = (Enum)Enum.Parse(_enumType, valueName, true);
                stringValue = GetStringValue(enumType);
            }
            catch (Exception) { }//Swallow!
            return stringValue;
        }

        public Type EnumType
        {
            get { return _enumType; }
        }

        #endregion

        #region Static implementation

        public static string GetStringValue(Enum value)
        {
            string output = null;
            Type type = value.GetType();
            //if (_stringValues.ContainsKey(value))
            //    output = (_stringValues[value] as StringValueAttribute).Value;
            //else
            //{
            //Look for our 'StringValueAttribute' in the field's custom attributes
            FieldInfo fi = type.GetField(value.ToString());
            StringValueAttribute[] attrs = fi.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];
            if (attrs.Length > 0)
            {
                // _stringValues.Add(value, attrs[0]);
                output = attrs[0].Value;
            }
            //}
            return output;
        }

        #endregion
    }
}
